/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2013-2014 sagyf Yang. The Four Group.
 */

define(['adminapp', 'core/datatable', 'bootstrap-datepicker', 'bootstrap-datepicker-zh'], function ($app) {
    $app.navactive('member');

    var doms = {
        action   : '#action_template',
        status   : '#status_template'
    };
    var action_tpl = Handlebars.compile($(doms.action).html());
    var status_tpl = Handlebars.compile($(doms.status).html());

    var _grid = new Datatable();
    _grid.init({
        src      : $("#datatable"),
        onSuccess: function (grid) {
            $('.date-picker').datepicker({
                language      : 'zh-CN',
                rtl: App.isRTL(),
                autoclose: true,
                format: 'yyyy-mm-dd'
            });
        },
        onError  : function (grid) {
        },
        dataTable: {
            "bServerSide"   : true, 'iDisplayLength': 50,
            "sAjaxSource"   : g.ctx + 'customer/integralRecord/list',
            "aaSorting"     : [
                [ 1, "asc" ]
            ],
            'aoColumns'   : [
                $app.checkall,
                $app.autono,
                {'mDataProp': 'id' },
                {'mDataProp': 'name' },
                {'mDataProp': 'card_no' },
                {'mDataProp': 'phone' },
                {'mDataProp': 'record_time' },
                {'mDataProp':  null },
                {'mDataProp':  null },
                {'mDataProp':  null },
                {'mDataProp':  null },
                {'mDataProp':  "resume" }
            ],
            'aoColumnDefs': [
                { 'bVisible': false, 'aTargets': [ 2 ] },
                {
                    'aTargets' : [7],
                    'mData'    : null,
                    "bSortable": false,
                    'mRender'  : function (data, type, full) {
                        return full['subtraction'] == 1 ? '<span class="badge badge-info">' + full['integral'] + '</span>' : '';
                    }
                },
                {
                    'aTargets' : [8],
                    'mData'    : null,
                    "bSortable": false,
                    'mRender'  : function (data, type, full) {
                        return full['subtraction'] == 0 ? '<span class="badge badge-warning">' + full['integral'] + '</span>' : '';
                    }
                },
                {
                    'aTargets' : [9],
                    'mData'    : null,
                    "bSortable": false,
                    'mRender'  : function (data, type, full) {
                        return full['subtraction'] == 1 ? full['old_integral'] + full['integral'] : full['old_integral'] - full['integral'];
                    }
                },
                {
                    'aTargets' : [10],
                    'mData'    : null,
                    "bSortable": false,
                    'mRender'  : function (data, type, full) {
                        return status_tpl(full);
                    }
                },
                {
                    'aTargets': [12],
                    'mData'   : null,
                    'mRender' : function (data, type, full) {
                        return action_tpl(full);
                    }
                }
            ],
            'fnRowCallback'  : function (nRow, aData, iDisplayIndex) {
                var oSettings = _grid.getDataTable().fnSettings();
                $("td:eq(1)", nRow).html(oSettings._iDisplayStart+iDisplayIndex +1);
                return nRow;
            }
        }
    });

    $("#datatable").delegate('a[name="confirm"]', 'click', function() {
        var id = $(this).attr('data-id');
        if (id) {
            bootbox.dialog({
                message: '确认本次积分操作么？',
                title: "积分确认",
                buttons: {
                    success: {
                        label: "确定",
                        className: "btn-success",
                        callback: function() {
                            $.ajax({
                                url : g.ctx + 'customer/integralRecord/confirm',
                                data: {id : id},
                                async: false,
                                success: function(data) {
                                    if (data.status == 'OK') {
                                        //window.location.reload();
                                        _grid.getTable().fnDraw();
                                    } else {
                                        bootbox.alert(data.message);
                                    }
                                }
                            });
                        }
                    },
                    danger: {
                        label: "取消",
                        className: "btn-danger",
                        callback: function() {
                            //Example.show("uh oh, look out!");
                        }
                    }
                }
            });
        }
    });

    return {};
});